package com.intel.factorytest.activity;


import com.intel.factorytest.R;
import android.os.Bundle;
import android.content.Context;
import android.os.PowerManager;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.app.KeyguardManager;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;
import android.content.Intent;
import java.io.FileOutputStream;
import android.view.WindowManager;

public class BacklightTest extends BaseActivity {


       private static final String TAG = "BacklightTest";
       private static final int CLOSE_BACKLIGHT = 0;
       private static final int OPEN_BACKLIGHT = 1;
       private static final long TIME_DELAY = 1200;
       private PowerManager.WakeLock backLightWL;
       private PowerManager pm;
       KeyguardManager.KeyguardLock keyguardLock;
       private int count = 0;
       private Button mSucBtn;
       private Button mFailBtn;
       private Button mRepeatBtn;
//the keypad backlight
       final byte[] ON = { '1', '0', '2' };
       final byte[] OFF = { '0' };
       private static String KEYPAD_BACKLIGHT_NODE = "/sys/class/leds/button-backlight/brightness";
//the lcd backlight
       private static String LCD_BACKLIGHT_NODE="/sys/class/leds/lcd-backlight/brightness";

       public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         setContentView(R.layout.backlight_test);
         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
         initView();
         count = 0;
         mSucBtn = (Button) findViewById(R.id.success_button);
         mFailBtn = (Button) findViewById(R.id.fail_button);
         mRepeatBtn = (Button) findViewById(R.id.repeat_button);
/*         mSucBtn.setEnabled(false);
         mFailBtn.setEnabled(false);
         mRepeatBtn.setEnabled(false);*/
         mRepeatBtn.setOnClickListener(new View.OnClickListener() {

              @Override
              public void onClick(View v) {
                 // TODO Auto-generated method stub
                 count = 0;
                 Message msg = new Message();
                 msg.what = CLOSE_BACKLIGHT;
                 myHandler.sendMessageDelayed(msg,TIME_DELAY);

              }
         });


         pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
         backLightWL = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP,"BacklightTest");//
         KeyguardManager keyguardManager = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE);
         keyguardLock = keyguardManager.newKeyguardLock("BacklightTest");
         Message msg = new Message();
         msg.what = CLOSE_BACKLIGHT;
         myHandler.sendMessageDelayed(msg,TIME_DELAY);
     }




     protected void initView() {
          super.initView();
      }

     Handler myHandler = new Handler(){
       public void handleMessage(Message msg){
          mItemTestFinish = true;
          switch(msg.what)
          {
              case CLOSE_BACKLIGHT:
                 Log.d(TAG," wangsm close backlight");
                 count++;
                 //enableDeviceBackLight(KEYPAD_BACKLIGHT_NODE, false);
                 enableDeviceBackLight(LCD_BACKLIGHT_NODE, false);
                 Message msg1 = new Message();
                 msg1.what = OPEN_BACKLIGHT;
                 myHandler.sendMessageDelayed(msg1, TIME_DELAY);
              break;

              case OPEN_BACKLIGHT:
                  Log.d(TAG," wangsm open backlight");
                  //enableDeviceBackLight(KEYPAD_BACKLIGHT_NODE, true);
                  enableDeviceBackLight(LCD_BACKLIGHT_NODE, true);
                  Message msg2 = new Message();
                  msg2.what = CLOSE_BACKLIGHT;
                  myHandler.sendMessageDelayed(msg2, TIME_DELAY);
/*                  if(count < 2){
                      Message msg2 = new Message();
                      msg2.what = CLOSE_BACKLIGHT;
                      myHandler.sendMessageDelayed(msg2, TIME_DELAY);
                  }else{
                        mItemTestFinish = true;
                       mSucBtn.setEnabled(true);
                       mFailBtn.setEnabled(true);
                       mRepeatBtn.setEnabled(true);
                  }*/
              break;
           }
       }
     };


     @Override
     public void onDestroy() {
         myHandler.removeMessages(CLOSE_BACKLIGHT);
         myHandler.removeMessages(OPEN_BACKLIGHT);
         enableDeviceBackLight(KEYPAD_BACKLIGHT_NODE, true);
         enableDeviceBackLight(LCD_BACKLIGHT_NODE, true);
         super.onDestroy();
     }

         private void enableDeviceBackLight(String path,boolean enable) {
                FileOutputStream fileOutputStream;
                try {
                   fileOutputStream = new FileOutputStream(path);
                   if (enable){
                      fileOutputStream.write(ON);
                   }else{
                      fileOutputStream.write(OFF);
                   }
                   fileOutputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }

}
